/*!
 * 一些工具
 */
if(!dateUtil){
	var dateUtil = {};
}

$.extend(dateUtil,{
	//格式化Date对象，默认格式：2001-12-31
	formatDate:function(formatDate, formatString) {  
		if(formatDate instanceof Date) {  
			var months = new Array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月");  
			var yyyy = formatDate.getFullYear();  
			var yy = yyyy.toString().substring(2);  
			var m = formatDate.getMonth();  
			var mm = m < 9 ? "0" + (m+1) : (m+1);  
			var mmm = months[m];  
			var d = formatDate.getDate();  
			var dd = d < 10 ? "0" + d : d;  
			
			var h = formatDate.getHours();  
			var hh = h < 10 ? "0" + h : h;  
			var n = formatDate.getMinutes();  
			var nn = n < 10 ? "0" + n : n;  
			var s = formatDate.getSeconds();  
			var ss = s < 10 ? "0" + s : s;  
			
			if(!formatString){
				formatString = "yyyy-mm-dd";
			}
			formatString = formatString.replace(/yyyy/i, yyyy);  
			formatString = formatString.replace(/yy/i, yy);  
			formatString = formatString.replace(/mmm/i, mmm);  
			formatString = formatString.replace(/mm/i, mm);  
			formatString = formatString.replace(/m/i, m+1);  
			formatString = formatString.replace(/dd/i, dd);  
			formatString = formatString.replace(/d/i, d);  
			formatString = formatString.replace(/hh/i, hh);  
			formatString = formatString.replace(/h/i, h);  
			formatString = formatString.replace(/nn/i, nn);  
			formatString = formatString.replace(/n/i, n);  
			formatString = formatString.replace(/ss/i, ss);  
			formatString = formatString.replace(/s/i, s);  
			
			return formatString;  
		} else {  
			return "";  
		}  
	}
	//返回偏移几天的时间，returnTypeIsDate是否要date格式的返回值，否则为格式2001-12-31
	,dayPlus:function(days,date,returnTypeIsDate){
		var result = (date && date instanceof Date) ? new Date(date.getTime()) : new Date();
		result.setDate(result.getDate()+days);
		if(returnTypeIsDate){
			return result;
		}else{
			return this.formatDate(result);
		}
	}
	//返回偏移几月的时间，returnTypeIsDate是否要date格式的返回值，否则为格式2001-12-31
	,monthPlus:function(months,date,returnTypeIsDate){
		var result = (date && date instanceof Date) ? new Date(date.getTime()) : new Date();
		result.setMonth(result.getMonth()+months);
		if(returnTypeIsDate){
			return result;
		}else{
			return this.formatDate(result);
		}
	}
	//返回上一天
	,lastDay:function(date,returnTypeIsDate){
		var result = (date && date instanceof Date) ? new Date(date.getTime()) : new Date();
		return this.dayPlus(-1,date,returnTypeIsDate);
	}
	//返回7天前
	,lastWeek:function(date,returnTypeIsDate){
		var result = (date && date instanceof Date) ? new Date(date.getTime()) : new Date();
		return this.dayPlus(-7,date,returnTypeIsDate);
	}
	//返回本月第一天
	,thisMonthBegin:function(date,returnTypeIsDate){
		var result = (date && date instanceof Date) ? new Date(date.getTime()) : new Date();
		result.setDate(1);
		return this.formatDate(result);
	}
	//返回上月第一天
	,lastMonthBegin:function(date,returnTypeIsDate){
		var result = (date && date instanceof Date) ? new Date(date.getTime()) : new Date();
		result.setDate(1);
		return this.monthPlus(-1,result,returnTypeIsDate);
	}
	//返回上月最后一天
	,lastMonthEnd:function(date,returnTypeIsDate){
		var result = (date && date instanceof Date) ? new Date(date.getTime()) : new Date();
		result.setDate(1);
		return this.dayPlus(-1,result,returnTypeIsDate);
	}
});

